﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace ChineseCheselrm1
{
    public partial class FrmMain : Form
    {
        //游戏对象说明
        private Game _game;
        private GameSide _game1;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void gameAera_Load(object sender, EventArgs e)
        {
            //初始化游戏类    
            _game = new Game();
            //播放游戏开始音乐
            SoundPlayer player = new SoundPlayer(Properties.Resources.开始游戏);
            player.Play();
            _game.gameChanged += _game_gameChanged;          
        }

        private void _game_gameChanged()
        {
            //重绘游戏区域
            if (_game != null)
            {
                gameAera.Invalidate();
                gameTimeAndSide.Invalidate();
            }
        }
        private void gameAera_Paint(object sender, PaintEventArgs e)
        {
            //游戏已经初始化了
            if (_game != null)
            {
                _game.Draw(e.Graphics, gameAera.Size);
            }
        }

        private void gameAera_MouseDown(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button == MouseButtons.Left)
            {
                //调用game的鼠标点击事件
                _game.MouseDown(e.Location, this.gameAera.Size);
                //重绘游戏
                this.gameAera.Invalidate();
                this.gameTimeAndSide.Invalidate();

            }

        }

        private void gameAera_MouseMove(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button == MouseButtons.Left)
            {
                //调用game的鼠标移动事件
                _game.MouseMove(e.Location, this.gameAera.Size);
                //重绘游戏
                this.gameAera.Invalidate();
                this.gameTimeAndSide.Invalidate();

            }
        }

        private void gameAera_MouseUp(object sender, MouseEventArgs e)
        {
            if (_game != null && e.Button == MouseButtons.Left)
            {
                //调用game的鼠标放开事件
                _game.MouseUp(e.Location, this.gameAera.Size);
                //重绘游戏
                this.gameAera.Invalidate();
                this.gameTimeAndSide.Invalidate();
                //移动合法则记录棋谱
                if (_game.Flag == true)
                {
                    listBoxChessScore.Items.Add(_game.MessageStr);
                    _game1.NextIsBlack = _game.ThisSide != Gameside.红;
                }
                //处理输赢
                if (_game.IsGameOver() == 0)
                {
                    //播放游戏结束音乐
                    SoundPlayer playerover = new SoundPlayer(Properties.Resources.游戏结束);
                    playerover.Play();
                    //停掉时钟
                    timerOver.Enabled = false;
                    //游戏结束
                    MessageBox.Show("红方获胜！");
                    //重置游戏
                    _game = new Game();
                    _game1 = new GameSide();
                    //播放游戏开始音乐
                    SoundPlayer playerstart = new SoundPlayer(Properties.Resources.开始游戏);
                    playerstart.Play();
                    //重绘游戏区域
                    this.gameAera.Invalidate();
                    this.gameTimeAndSide.Invalidate();
                    //清除listbox
                    listBoxChessScore.Items.Clear();
                    //开启时钟
                    timerOver.Enabled = true;
                }
                else if (_game.IsGameOver() == 1)
                {
                    //播放游戏结束音乐
                    SoundPlayer playerover = new SoundPlayer(Properties.Resources.游戏结束);
                    playerover.Play();
                    //停掉时钟
                    timerOver.Enabled = false ;
                    //游戏结束
                    MessageBox.Show("黑方获胜！");
                    //重置游戏
                    _game = new Game();
                    _game1 = new GameSide();
                    //播放游戏开始音乐
                    SoundPlayer playerstart = new SoundPlayer(Properties.Resources.开始游戏);
                    playerstart.Play();
                    //重绘游戏区域
                    this.gameAera.Invalidate();
                    this.gameTimeAndSide.Invalidate();
                    //清除listbox
                    listBoxChessScore.Items.Clear();
                    //开启时钟
                    timerOver.Enabled = true;
                }

            }
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {

            //重置游戏
            if (_game != null)
            {
                MessageBox.Show("你真的要重新开始吗？");
                //播放游戏开始音乐
                SoundPlayer player = new SoundPlayer(Properties.Resources.开始游戏);
                player.Play();
                _game = new Game();
                _game1 = new GameSide();
                timerOver = new Timer();
                //重绘游戏区域
                gameAera.Invalidate();
                gameTimeAndSide.Invalidate();
                //清除listbox
                listBoxChessScore.Items.Clear();
                //启动时钟
                timerOver.Enabled = true;
            }
        }

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _game = new Game();
            _game1 = new GameSide();
            _game.gameChanged += _game_gameChanged;
        }

        private void gameTimeAndSide_Load(object sender, EventArgs e)
        {
            //初始化游戏类
            _game1 = new GameSide();
            this.timerOver.Interval = 1000;
            this.timerOver.Start();
        }
        private void gameTimeAndSide_Paint(object sender, PaintEventArgs e)
        {
            //游戏已经初始化了
            if (_game1 != null)
            {
                _game1.Draw(e.Graphics, gameTimeAndSide.Size);
            }
        }


        private void panelleft2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_game != null)
            {
                TimeSpan ts = _game.NextTime - System.DateTime.Now;
                this.labeltime.Text = "步时剩余：" + ts.ToString("mm") + "分" + ts.ToString("ss") + "秒";
                //超时，游戏结束
                int mint = ts.Minutes;
                int seco = ts.Seconds;
                //时间到了，游戏结束
                if (mint == 0 && seco == 0 && _game.ThisSide == Gameside.红)
                {
                    //播放游戏结束音乐
                    SoundPlayer playerover = new SoundPlayer(Properties.Resources.游戏结束);
                    playerover.Play();
                    //停止timer工作
                    timerOver.Enabled = false;
                    MessageBox.Show("红方超时判负！");
                    //重置一个新的游戏
                    _game = new Game();
                    _game1 = new GameSide();
                    //播放游戏开始音乐
                    SoundPlayer playerstart = new SoundPlayer(Properties.Resources.开始游戏);
                    playerstart.Play();
                    //重绘游戏区域
                    this.gameAera.Invalidate();
                    this.gameTimeAndSide.Invalidate();
                    //清除listbox
                    listBoxChessScore.Items.Clear();
                    //时钟重新开始
                    timerOver.Enabled = true;
                }
                else if (mint == 0 && seco == 0 && _game.ThisSide == Gameside.黑)
                {
                    //播放游戏结束音乐
                    SoundPlayer playerover = new SoundPlayer(Properties.Resources.游戏结束);
                    playerover.Play();
                    //停止时钟工作
                    timerOver.Enabled = false;
                    MessageBox.Show("黑方超时判负！");
                    //重新开始新的游戏
                    _game = new Game();
                    _game1 = new GameSide();
                    //播放游戏开始音乐
                    SoundPlayer playerstart = new SoundPlayer(Properties.Resources.开始游戏);
                    playerstart.Play();
                    //重绘游戏区域
                    this.gameAera.Invalidate();
                    this.gameTimeAndSide.Invalidate();
                    //清除listbox
                    listBoxChessScore.Items.Clear();
                    //时钟重新开始
                    timerOver.Enabled = true;
                }

            }

        }
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btndanma_Click(object sender, EventArgs e)
        {
          
        }
    }
}
